// Copyright (c) 2023 Institute of Software, Chinese Academy of Sciences
// Ripple is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//          http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.

package ripple.common.tcp.decoder;

import io.netty.buffer.ByteBuf;
import ripple.common.entity.Constants;
import ripple.common.entity.NodeMetadata;
import ripple.common.entity.Reform;
import ripple.common.helper.TypeHelper;
import ripple.common.tcp.Decoder;
import ripple.common.tcp.Message;
import ripple.common.tcp.MessageType;
import ripple.common.tcp.message.ReformSyncRequest;
import ripple.common.tcp.message.SyncRequest;

import java.util.Date;

public class ReformSyncRequestDecoder implements Decoder {
    @Override
    public Message decode(ByteBuf byteBuf, MessageType messageType) {
        ReformSyncRequest reformSyncRequest = new ReformSyncRequest();
        reformSyncRequest.setUuid(TypeHelper.readUuid(byteBuf));
        reformSyncRequest.setMessageUuid(TypeHelper.readUuid(byteBuf));
        reformSyncRequest.setOperationType(TypeHelper.readString(byteBuf));
        if (reformSyncRequest.getOperationType().equals(Constants.REFORM_COMMIT)) {
            reformSyncRequest.setNodeList(NodeMetadata.ListFromString(TypeHelper.readString(byteBuf)));
//        } else if (reformSyncRequest.getOperationType().equals(Constants.MESSAGE_TYPE_INCREMENTAL_UPDATE)) {
//            reformSyncRequest.setBaseMessageUuid(TypeHelper.readUuid(byteBuf));
//            reformSyncRequest.setAtomicOperation(TypeHelper.readString(byteBuf));
//            reformSyncRequest.setValue(TypeHelper.readString(byteBuf));
        }
        reformSyncRequest.setLastUpdate(new Date(byteBuf.readLong()));
        reformSyncRequest.setLastUpdateServerId(byteBuf.readInt());
        return reformSyncRequest;
    }
}